import axios from 'axios'
import qs from 'qs'
import _this from '../main.js';


// axios 配置
// axios.defaults.timeout = 8000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.headers.post['x-requested-with'] = 'xmlhttprequest'; //ajax请求
axios.defaults.headers.get['x-requested-with'] = 'xmlhttprequest'; //ajax请求
axios.defaults.headers.common['ZT-Mobile'] = 'weixin';

axios.defaults.baseURL = '/api';

// post传参序列化
axios.interceptors.request.use((config) => {
  if (config.method === 'post') {
    config.data = qs.stringify(config.data)
  }
  return config;
}, (error) => {
  _this.$vux.toast.text('请求超时', 'top', '800')
  return Promise.resolve(error);
});

// 返回状态判断
axios.interceptors.response.use(response => {
  return response
}, error => {
  if (error && error.response) {
    switch (error.response.status) {
      case 400:
        error.message = '请求错误'
        break

      case 401:
        error.message = '未授权，请登录'
        break

      case 403:
        error.message = '拒绝访问'
        break

      case 404:
        error.message = `请求地址出错: ${err.response.config.url}`
        break

      case 408:
        error.message = '请求超时'
        break

      case 500:
        error.message = '服务器内部错误'
        break

      case 501:
        error.message = '服务未实现'
        break

      case 502:
        error.message = '网关错误'
        break

      case 503:
        error.message = '服务不可用'
        break

      case 504:
        error.message = '网关超时'
        break

      case 505:
        error.message = 'HTTP版本不受支持'
        break

      default:
    }
  }
  _this.$vux.toast.text(error.message, 'top', '800')
  return Promise.resolve(error);
})

export function fetch(url, params, method, config) {
  return new Promise((resolve, reject) => {
    if (method == 'get') {
      axios({
          url: url,
          params: params,
          method: method
        }).then(response => {
          resolve(response.data);
        }, err => {
          reject(err);
        })
        .catch((error) => {
          reject(error);
        })
    } else if (method == 'post') {
      axios.post(url, params, config).then(response => {
          resolve(response.data);
        }, err => {
          reject(err);
        })
        .catch((error) => {
          reject(error)
        })
    }
  })
}

//后台数据请求链接
export default {
  //登录
  login(params) {
    return fetch('/login/login/index', params, 'post')
  },

  //个人资料
  person_info(params) {
    return fetch('/user/index/index', params, 'post')
  },
  //个人承诺
  changepromise(params) {
    return fetch('/user/index/do_promise', params, 'post')
  },
  //修改密码
  changepassword(params) {
    return fetch('/user/index/mend_password', params, 'post')
  },
  //党支部列表
  contact(params) {
    return fetch('/user/index/department_list', params, 'post')
  },
  //根据department_id查询用户列表
  queryuser(params) {
    return fetch('/user/index/user_list', params, 'post')
  },


  //首页轮播图
  get_banner_list(params) {
    return fetch('/djdt/swipe/index', params, 'post')
  },
  //党建动态
  get_home_lists(params) {
    return fetch('/djdt/inform/index', params, 'post')
  },
  //党建动态详情
  get_home_detail(params) {
    return fetch('/djdt/inform/detail', params, 'post')
  },
  //消息中心
  get_news_lists(params) {
    return fetch('/home/message/index', params, 'post')
  },
  post_news_read(params) {
    return fetch('/home/message/read', params, 'post')
  },
  get_unnews_nums(params) {
    return fetch('/home/message/count', params, 'post')
  },
  //支部风采
  get_branch_list(params) {
    return fetch('/zbfc/department/index', params, 'post')
  },
  //支部信息
  get_branch_infos(params) {
    return fetch('/zbfc/department/detail', params, 'post')
  },


  //三会一课搜索支部
  search_branch_list(params) {
    return fetch('/shyk/meeting/search', params, 'post')
  },


  //三会一课
  get_lessons_list(params) {
    return fetch('/shyk/meeting/getMeeting', params, 'post')
  },
  //统计分析——支部课程
  get_lessons_branch(params) {
    return fetch('/shyk/meeting/search', params, 'post')
  },
  //支部课程
  get_branch_lessons(params) {
    return fetch('/shyk/meeting/open', params, 'post')
  },

  //支部课程
  get_branch_lessons_select(params) {
    return fetch('/shyk/meeting/search', params, 'post')
  },
  //会议详情
  get_lesson_detail(params) {
    return fetch('/shyk/meeting/detail', params, 'post')
  },

  //走帮服
  get_helps_list(params) {
    return fetch('/zbf/help/index', params, 'post')
  },
  //走帮服详情
  get_help_detail(params) {
    return fetch('/zbf/help/detail', params, 'post')
  },
  //走帮服——统计分析
  get_helps_branch(params) {
    return fetch('/zbf/help/searchDepartment', params, 'post')
  },
  get_helps_person(params) {
    return fetch('/zbf/help/situation', params, 'post')
  },
  get_person_helps(params) {
    return fetch('/zbf/help/getList', params, 'post')
  },
  get_help_branch_select(params) {
    return fetch('/zbf/help/searchDepartment', params, 'post')
  },
  get_help_person_select(params) {
    return fetch('/zbf/help/searchUser', params, 'post')
  },

  //党费缴纳
  get_fees_branch(params) {
    return fetch('/dfgl/list/searchDepartment', params, 'post')
  },
  //党员缴纳
  get_fees_person(params) {
    return fetch('/dfgl/list/user', params, 'post')
  },
  get_fees_branch_select(params) {
    return fetch('/dfgl/list/searchDepartment', params, 'post')
  },
  get_fees_person_select(params) {
    return fetch('/dfgl/list/searchUser', params, 'post')
  },
  //党员缴纳明细
  get_fees_details(params) {
    return fetch('/dfgl/list/getUserDues', params, 'post')
  },
  //积分明细
  get_integral_details(params) {
    return fetch('/jfgl/detail/detail', params, 'post')
  },
  //党员季度积分
  get_integral_list(params) {
    return fetch('/jfgl/rank/quarter', params, 'post')
  },
  get_integral_list2(params) {
    return fetch('/jfgl/rank/annual', params, 'post')
  },
  get_quarter_select(params) {
    return fetch('/jfgl/rank/searchQuarterDepartment', params, 'post')
  },
  get_year_select(params) {
    return fetch('/jfgl/rank/searchAnnualDepartment', params, 'post')
  },

  //党员发展
  get_party_develop_list(params) {
    return fetch('/dyfz/development/index', params, 'post')
  },
  //申请人数
  get_apply_num(params) {
    return fetch('/dyfz/development/getNum', params, 'post')
  },
  //获取申请人信息
  get_apply_info(params) {
    return fetch('/dyfz/development/getUserInfo', params, 'post')
  },
  get_develop_select(params) {
    return fetch('/dyfz/development/search', params, 'post')
  },

  //示范党课
  get_demos_list(params) {
    return fetch('/sfdk/lesson/index', params, 'post')
  },
  //示范党课统计分析
  get_demos_person(params) {
    return fetch('/sfdk/lesson/analysis', params, 'post')
  },
  //党课列表
  get_person_demo(params) {
    return fetch('/sfdk/lesson/getUserLesson', params, 'post')
  },
  //党课详情
  get_demo_detail(params) {
    return fetch('/sfdk/lesson/detail', params, 'post')
  },
  get_demos_select(params) {
    return fetch('/sfdk/lesson/search', params, 'post')
  },

  //主题党日
  get_themes_list(params) {
    return fetch('/shyk/meeting/index', params, 'post')
  },
  //主题党日详情
  get_theme_detail(params) {
    return fetch('/shyk/meeting/detail', params, 'post')
  },

  //党员心声发布记录列表
  get_record_list(params) {
    return fetch('/dyxs/problem/record', params, 'post')
  },
  //党员心声发布记录详情
  get_record_details(params) {
    return fetch('/dyxs/problem/detail', params, 'post')
  }

}
